package com.easychat.service.impl;

import com.easychat.entity.dto.MessageSendDto;
import com.easychat.entity.dto.TokenUserInfoDto;
import com.easychat.entity.enums.MessageTypeEnum;
import com.easychat.entity.enums.UserContactStatusEnum;
import com.easychat.entity.po.ChatMessage;
import com.easychat.entity.po.UserInfo;
import com.easychat.entity.query.UserInfoQuery;
import com.easychat.mappers.UserInfoMapper;
import com.easychat.service.UserInfoService;
import com.easychat.utils.CopyTools;
import com.easychat.websocket.netty.ChannelContextUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Dongdong
 * @注释
 */
@Service
public class GetOnlineServiceImpl implements com.easychat.service.GetOnlineService {


    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private ChannelContextUtils channelContextUtils;

    @Override
    public void pushFriend(TokenUserInfoDto tokenUserInfo,boolean isOnline) {
        String userId = tokenUserInfo.getUserId();

        UserInfoQuery userInfoQuery = new UserInfoQuery();
        userInfoQuery.setUserId(userId);
        userInfoQuery.setStatus(String.valueOf(UserContactStatusEnum.FRIEND.getCode()));

        List<UserInfo> friends = userInfoMapper.selectList(userInfoQuery);

        for (UserInfo friend : friends) {
            ChatMessage chatMessage = new ChatMessage();
            chatMessage.setMessageContent("您的好友"+tokenUserInfo.getNickName()+"上线了");
            chatMessage.setSendUserId(userId);
            chatMessage.setContactId(friend.getUserId());
            chatMessage.setMessageType(MessageTypeEnum.ONLINE_FRIEND.getType());

            MessageSendDto messageSendDto = CopyTools.copy(chatMessage, MessageSendDto.class);
            channelContextUtils.sendMessage(messageSendDto);
        }


    }

}
